package cn.toutatis.sandbox.root.config;

import cn.org.rapid_framework.freemarker.directive.BlockDirective;
import cn.org.rapid_framework.freemarker.directive.ExtendsDirective;
import cn.org.rapid_framework.freemarker.directive.OverrideDirective;
import cn.toutatis.sandbox.root.systemService.entity.SystemDictionary;
import cn.toutatis.sandbox.root.systemService.service.SystemDictionaryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import freemarker.template.TemplateModelException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.List;

@Configuration
public class FreemarkerConfig {

    @Autowired
    freemarker.template.Configuration configuration;

    @Autowired
    SystemDictionaryService systemDictionaryService;

    @Bean
    public HashMap globalMap() throws TemplateModelException {
        configuration.setSharedVariable("block", new BlockDirective());
        configuration.setSharedVariable("override", new OverrideDirective());
        configuration.setSharedVariable("extends", new ExtendsDirective());
        HashMap<String ,Object> map = new HashMap<>(8);
        QueryWrapper<SystemDictionary> parentWrapper = new QueryWrapper<>();
        parentWrapper.eq("code","SYSTEM_STATUS_0000");
        SystemDictionary parent = systemDictionaryService.getOne(parentWrapper);
        Integer id = parent.getId();
        QueryWrapper<SystemDictionary> dataStatusWrapper = new QueryWrapper<>();
        dataStatusWrapper.eq("parentId",id);
        List<SystemDictionary> dataStatus = systemDictionaryService.list(dataStatusWrapper);
        configuration.setSharedVariable("dataStatus",dataStatus);
        configuration.setSharedVariable("normalSign","SYS_OPEN_0000");
        map.put("dataStatus",dataStatus);
        map.put("normalSign","SYS_OPEN_0000");
        map.put("closeSign","SYS_CLOSE_0000");
        return map;
    }
}
